#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE 5

/* function declaration */

void print_menu();
int revstring(char*, int);
void callrevstring();
void analyzestr(char, int);
void callanalyzestr();

/* main function */

int main(){
	int key, cont = 1;
	print_menu();
	while  (cont != 0){
		scanf("%d", &key);
		switch (key){
			case 1:{
				system("cls");
				callrevstring();
				print_menu();
				break;
				   }

			case 2:{
				system("cls");
				callanalyzestr();
				print_menu();
				break;
				   }
			default:{
				system("cls");
				return 0;
					}
		}
	}
}


void print_menu(){
	printf("Welcome to Exercise \n");
	printf("1- Reverse String\n");
	printf("2- Analyze string (Counts capital letters, small letters, numbers, characters) \n");
	printf("0- Exit\n");
}

void callrevstring(){
   char inputstr [SIZE];
   int capchars, smallchars, numchars, otherchars;
   printf("Enter string\n");
   gets(inputstr);
   revstring(inputstr, SIZE);
}

int revstring(char *str, int size){
	int flag = 0, res = 0;
	char *endptr, *startptr, temp;
	printf("Input String: %s\n", str);
	startptr = &str[0];
	endptr = &str[size-1];
	while (endptr >= startptr){
		if(*endptr == *startptr){
			if ((flag == 1) && (startptr >= endptr)){
				printf("Same string!!!!\n");
				return 1;
			}
			flag = 1;
			endptr--;
			startptr++;
			continue;
		}
		flag = 0;
		temp = *endptr;
		*endptr = *startptr;
		*startptr = temp;
		endptr--;
		startptr++;
	}
	printf("Reversed input string: %s\n", str);
	return 0;

}

void callanalyzestr(){
	char inputstr [SIZE];
	int cap = 0, small = 0, num = 0, other = 0;
	printf("Enter string\n");
    gets(inputstr);
    analyzestr(inputstr, SIZE, &cap, &small, &num, &other);
	printf("Your string is compound by:\n");
	printf("Capital letters: %d\n", cap);
	printf("Small letters: %d\n", small);
	printf("Numbers: %d\n", num);
	printf("Other letters: %d\n", other);
	printf("Total letters: %d \n\n", (cap + small + num + other));
}

void analyzestr(char *str, int size, int* cap, int* small, int* num, int* other){
	int i;
	for(i = 0; i < size; i++){
		if (isdigit(str[i])){
			(*num)++;
		}
		else if (islower(str[i])){
				(*small)++;
			}
			else if (isupper(str[i])){
				(*cap)++;
			}
			else{
				(*other)++;
			}
	}
}
